package com.zzyl.controller.security;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    RoleService roleService;

    @PostMapping("/init-roles")
    public ResponseResult<List<RoleVo>> initrole() {
        List<RoleVo> dto = roleService.initrole();
        return ResponseResult.success(dto);
    }

    //TODO @ApiOperation("角色分页")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageBean<RoleVo>> query(@RequestBody RoleDto dto, @PathVariable("pageNum") int pageNum,
                                                  @PathVariable("pageSize") int pageSize) {
        PageBean<RoleVo> vo = roleService.query(dto, pageNum, pageSize);
        return ResponseResult.success(vo);
    }

    //TODO @ApiOperation("角色添加")
    @PutMapping
    public ResponseResult<Boolean> addrole(@RequestBody RoleDto dto) {
        roleService.add(dto);
        return ResponseResult.success(true);
    }

    //TODO @ApiOperation("根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<Set<String>> select(@PathVariable Long roleId) {
        Set<String> vo = roleService.selectid(roleId);
        return ResponseResult.success(vo);
    }

    //TODO @ApiOperation("角色修改")
    @PatchMapping
    public ResponseResult<Boolean> update(@RequestBody RoleDto dto) {
        roleService.update(dto);
        return ResponseResult.success(true);
    }

    //TODO @ApiOperation("删除角色")
    @DeleteMapping("/{roleIds}")
    public ResponseResult<Void> delete(@PathVariable Long roleIds) {
        roleService.delete(roleIds);
        return ResponseResult.success();
    }

    //TODO @ApiOperation("查询所有角色")
    @GetMapping
    public ResponseResult<List<RoleVo>> selectall() {
        List<RoleVo> vos = roleService.selectall();
        return ResponseResult.success(vos);
    }
}
